//
//  SWEventPhotosView.swift
//  ChildStory
//
//  Created by linhan on 2016/11/25.
//  Copyright © 2016年 Aiya. All rights reserved.
//

import Foundation
import UIKit

protocol SWEventPhotosViewDelegate:NSObjectProtocol
{
    func eventPhotosViewWillPresent(view:SWEventPhotosView)
    func eventPhotosViewWillDismiss(view:SWEventPhotosView)
    func eventPhotosViewShare(view:SWEventPhotosView)
}

class SWEventPhotosView: UIView
{
    weak var delegate:SWEventPhotosViewDelegate?
    
    let leftMargin:CGFloat = 12
    let rightMargin:CGFloat = 12
    let topMargin:CGFloat = 8
    
    var hCollectionView:UICollectionView?
    var vCollectionView:UICollectionView?
    
    private var _vCollectionViewY:CGFloat = 0
    private var _state:SWPopupContainerState = .closed
    private var _toolbar:UIView = UIView()
    
    
    
    //返回当前打开关闭状态
    var state:SWPopupContainerState
    {
        return _state
    }
    
    init() {
        super.init(frame: CGRect.zero)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setup(containerWidth:CGFloat)
    {
        clipsToBounds = true
        backgroundColor = UIColor(hex: 0xf5f5f5)
        frame = CGRectMake(0, 0, containerWidth, 0)
        
        let hContainer:UIView = UIView()
        hContainer.backgroundColor = UIColor.white
        hContainer.frame = CGRectMake(0, 0, containerWidth, 117)
        addSubview(hContainer)
        
        let hIcon:UIImageView = UIImageView(image:UIImage(named:"event_detail_img_favorite"))
        hIcon.frame = CGRectMake(leftMargin, topMargin, hIcon.width, hIcon.height)
        hContainer.addSubview(hIcon)
        
        let hCollectionViewFrame = CGRectMake(hIcon.right, topMargin, containerWidth - hIcon.right, 85)
        createHCollectionView(container: hContainer, frame: hCollectionViewFrame)
        
        
        _vCollectionViewY = hContainer.bottom + 8
        createVCollectionView(y: _vCollectionViewY, containerWidth: containerWidth)
        
        _toolbar.backgroundColor = UIColor.white
        _toolbar.frame = CGRectMake(0, 0, width, 40)
        addSubview(_toolbar)
        
        let shareBtn:UIButton = UIButton(type: .custom)
        shareBtn.setImage(UIImage(named:"event_detail_btn_share"), for: .normal)
        shareBtn.sizeToTouchEasy()
        shareBtn.center = CGPointMake(containerWidth * 0.25, _toolbar.height * 0.5)
        shareBtn.addTarget(self, action: #selector(shareBtnTapped), for: .touchUpInside)
        _toolbar.addSubview(shareBtn)
        
        let collapseBtn:UIButton = UIButton(type: .custom)
        collapseBtn.setImage(UIImage(named:"event_detail_btn_collapse"), for: .normal)
        collapseBtn.sizeToTouchEasy()
        collapseBtn.center = CGPointMake(containerWidth * 0.75, _toolbar.height * 0.5)
        collapseBtn.addTarget(self, action: #selector(collapseBtnTapped), for: .touchUpInside)
        _toolbar.addSubview(collapseBtn)
    }
    
    //展开
    func expand(containerHeight:CGFloat)
    {
        if _state == .closed || _state == .closing
        {
            _state = .opening
            delegate?.eventPhotosViewWillPresent(view: self)
            self.height = containerHeight
            vCollectionView?.height = containerHeight - _toolbar.height - _vCollectionViewY
            _toolbar.y = containerHeight - _toolbar.height
            
            
            UIView.animate(withDuration: 0.3, animations: {
                self.alpha = 1
            }, completion: {(finish:Bool) in
                self._state = .opened
            })
            
        }//end state
    }
    
    
    //收起
    func collapse()
    {
        if _state == .opened || _state == .opening
        {
            _state = .closing
            
            delegate?.eventPhotosViewWillDismiss(view: self)
            
            UIView.animate(withDuration: 0.3, animations: {
                self.alpha = 0
            }, completion: {(finish:Bool) in
                self.height = 0
                self._state = .closed
            })
            
        }//end state
    }
    
    private func createHCollectionView(container:UIView, frame:CGRect)
    {
        let Grid:CGFloat = 3
        let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layout.itemSize = CGSizeMake(85, 85)
        
        layout.minimumLineSpacing = Grid
        layout.sectionInset = UIEdgeInsetsMake(0, Grid, 0, rightMargin)
        layout.scrollDirection = .horizontal
        
        hCollectionView = UICollectionView(frame: frame, collectionViewLayout: layout)
        hCollectionView?.backgroundColor = UIColor.white
        hCollectionView?.register(SWEventPhotoCollectionCell.self, forCellWithReuseIdentifier: "SWEventPhotoCollectionCell")
        container.addSubview(hCollectionView!)
    }
    
    private func createVCollectionView(y:CGFloat, containerWidth:CGFloat)
    {
        let numOfHorizontal:Int = 4
        let Grid:CGFloat = 3
        let cellWidth:CGFloat = (containerWidth - leftMargin - rightMargin - (CGFloat(numOfHorizontal - 1) * Grid)) / CGFloat(numOfHorizontal)
        let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layout.itemSize = CGSizeMake(cellWidth, cellWidth)
        //横向最小间隔
        layout.minimumInteritemSpacing = Grid
        //纵向最小间隔
        layout.minimumLineSpacing = Grid
        layout.sectionInset = UIEdgeInsetsMake(topMargin, leftMargin, 0, rightMargin)
        
        let collectionViewFrame = CGRectMake(0, y, containerWidth, 0)
        vCollectionView = UICollectionView(frame: collectionViewFrame, collectionViewLayout: layout)
        vCollectionView?.backgroundColor = UIColor.white
        vCollectionView?.register(SWEventPhotoCollectionCell.self, forCellWithReuseIdentifier: "SWEventPhotoCollectionCell")
        vCollectionView?.register(SWEventPhotoAddCollectionCell.self, forCellWithReuseIdentifier: "SWEventPhotoAddCollectionCell")
        addSubview(vCollectionView!)
    }
    
    @objc private func shareBtnTapped()
    {
        delegate?.eventPhotosViewShare(view: self)
    }
    
    @objc private func collapseBtnTapped()
    {
        collapse()
    }
    
    @objc private func extendBtnTapped()
    {
        if _state == .opened || _state == .opening
        {
            //delegate?.view(self, click: CallbackAction.Collapse, argument: nil)
        }
        else
        {
            //delegate?.view(self, click: CallbackAction.Expand, argument: nil)
        }
    }
    
    
}
